package com.scaffold.chapter.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import wangjg.commons.page.Page;

import com.scaffold.chapter.entity.Topic;
import com.scaffold.springmvc.dao.BasicBizDAO;

@Repository
public class TopicDAO extends BasicBizDAO<Topic,Long>{

	public List<Topic> pageQuery(Page page ) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

	public List<Topic> pageQuery(Page page, String title, String keyword) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		if(title != null){
		criteria.add(Restrictions.eq("title", title));
		}
		if(keyword!=null && !keyword.isEmpty()){
		criteria.add(Restrictions.like("title", keyword, MatchMode.ANYWHERE).ignoreCase());
		}		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}



}
